package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.UserBackMapper;
import com.zretc.entity.Account;
import com.zretc.service.UserBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserBackServiceImpl implements UserBackService {

    @Autowired
    private UserBackMapper userBackMapper;

    @Override
    public PageInfo<Account> getAccountPage(Map<String, String> params) {
        // 获取当前页数
        int pageNum = Integer.parseInt(params.get("pageNum"));
        // 获取条数
        int pageSize = Integer.parseInt(params.get("pageSize"));
        // 获取用户名
        String accountName = params.get("accountName");
        // 获取用户密码
        String accountId = params.get("accountId");

        // 1.开始分页
        PageHelper.startPage(pageNum,pageSize);

        // 2.调用查询方法
        // 对传递给dao方法中参数进行处理
        Map<String,String> map = new HashMap<>();

        if ("".equals(accountName) || accountName == null){
            map.put("accountName",null);
        }else{
            map.put("accountName","%" + accountName + "%");
        }

        if ("".equals(accountId) || accountId == null){
            map.put("accountId",null);
        }else{
            map.put("accountId", accountId);
        }

        List<Account> accountList = userBackMapper.findAccountList(map);

        // 3.包装到PageInfo对象
        PageInfo<Account> pageInfo =new PageInfo<>(accountList);

        return pageInfo;
    }

    @Override
    public boolean deleteByAccountId(String accountId) {

        // 调用dao方法
        int i = userBackMapper.deleteByAccountId(accountId);
        if(i != 0){
            return true;
        } else {
            return false;
        }

    }

    @Override
    public boolean updateAccount(Account account) {

        // 调用dao方法
        int i = userBackMapper.updateAccount(account);
        if(i != 0){
            return true;
        } else {
            return false;
        }

    }

    @Override
    public boolean insertAccount(Account account) {

        // 调用dao方法
        int i = userBackMapper.insertAccount(account);
        if(i != 0){
            return true;
        } else {
            return false;
        }
    }
}
